<template>
  <div class="app-container">
    <div class="container">
      <!--form 组件-->
      <eForm ref="form" :is-add="isAdd"/>
      <!--工具栏-->
      <div>
        <el-form ref="query" :inline="true" :model="query" size="small" class="demo-form-inline">
          <el-form-item label="规格名称：" prop="name">
            <el-input v-model="query.name" clearable placeholder="" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
            <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            <el-button
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="add">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--表格渲染-->
      <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
        <el-table-column label="规格名称" prop="name"/>
        <el-table-column label="排序" prop="sort"/>
        <el-table-column label="规格值" prop="value" show-overflow-tooltip/>
        <el-table-column label="操作" width="130px" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
            <el-popover
              :ref="scope.row.specs_id"
              placement="top"
              width="180">
              <p>确定删除本条数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.specs_id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.specs_id)">确定</el-button>
              </div>
              <el-button slot="reference" :disabled="scope.row.specs_id === 1" type="danger" icon="el-icon-delete" size="mini"/>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page + 1"
        background
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange" />
    </div>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { del } from '@/api/mall/specs'
import eForm from './form'
export default {
  components: { eForm },
  mixins: [initData],
  data() {
    return {
      isAdd: false,
      delLoading: false,
      query: {
        name: ''
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'api/specifications'
      const sort = 'sort,asc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const name = query.name
      if (name) { this.params['name'] = name }
      return true
    },
    resetQuery() {
      this.$refs['query'].resetFields()
      this.init()
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(row) {
      this.isAdd = false
      const _this = this.$refs.form
      const valueStr = row.value
      _this.dialog = true
      let value = ''
      const domains = []
      if (valueStr && valueStr.indexOf('、') !== -1) {
        const arr = valueStr.split('、')
        if (arr && arr.length > 1) {
          value = arr[0]
          for (let i = 1; i < arr.length; i++) {
            domains.push({ key: this.uuid(), value: arr[i] })
          }
        }
      } else if (valueStr) {
        value = valueStr
      }
      _this.form = { name: row.name, sort: row.sort, value: value, specs_id: row.specs_id, domains: domains, values: '' }
    },
    uuid() {
      const s = []
      const hexDigits = '0123456789abcdef'
      for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
      }
      s[14] = '4'
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
      s[8] = s[13] = s[18] = s[23] = '-'
      return s.join('')
    },
    subDelete(specs_id) {
      this.delLoading = true
      del(specs_id).then(res => {
        if (res.result) {
          this.delLoading = false
          this.$refs[specs_id].doClose()
          this.dleChangePage()
          this.init()
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
      }).catch(err => {
        this.delLoading = false
        this.$refs[specs_id].doClose()
        console.log(err.response.data.message)
      })
    }
  }
}
</script>
